﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections.ObjectModel;

namespace RollCallClient
{
    /// <summary>
    /// content user setting
    /// </summary>
    public class UserSetting
    {
        //username
        private String strUserName;
        //List of dates that user wanna check attendence
        private Collection<int> clDates = new Collection<int>();

        //Contructor have no parameter
        public UserSetting()
        {
        }
        /// <summary>
        /// user setting
        /// </summary>
        /// <param name="User"></param>
        public UserSetting(String strUserName)
        {
            this.strUserName = strUserName;
            this.clDates = null;
        }
        /// <summary>
        /// user setting
        /// </summary>
        /// <param name="user"></param>
        /// <param name="day"></param>
        public UserSetting(String user, Collection<int> clDates)
        {
            this.strUserName = user;
            this.clDates = clDates;
        }
        /// <summary>
        /// get username
        /// </summary>
        /// <returns></returns>
        public String GetUserName()
        {
            return strUserName;
        }
        /// <summary>
        /// Get list of dates were checked
        /// </summary>
        /// <returns></returns>
        public Collection<int> GetDayChecking()
        {
            return clDates;
        }
        /// <summary>
        /// Set username for user
        /// </summary>
        /// <param name="user"></param>
        public void SetUserName(String strUserName)
        {
            this.strUserName = strUserName;
        }
        /// <summary>
        /// set date checking for user
        /// </summary>
        /// <param name="day"></param>
        public void SetDayChecking(Collection<int> clDates)
        {
            this.clDates = clDates;
        }
        /// <summary>
        /// write user info to hard disk
        /// </summary>
        public void WriteUserInfoToFile()
        {
            //Implements a System.IO.TextWriter for writing characters to a stream in a
            //particular encoding, in detail, it point to file UserInfo.dat
            StreamWriter swFileWriter = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                + "\\RollCallClient\\UserInfo.dat");
            //Write username to file
            swFileWriter.WriteLine(this.strUserName);
            //Write dates was checked into next line of file,they were separate by ';'
            for (int i = 0; i < clDates.Count; i++)
            {
                swFileWriter.Write(clDates[i] + ";");
            }
            //close stream writer
            swFileWriter.Close();
        }
        /// <summary>
        /// read information from the file
        /// </summary>
        /// <returns></returns>
        public bool ReadUserInfoFromFile()
        {
            //temp variable for what was read from the first line of file (Username)
            String strFirstLine;
            //temp variable for what was read from the second line of file (Date was checked) 
            String strSecondLine;
            try
            {   //Implements a System.IO.TextReade for reading characters from file - UserInfo.dat
                //Used for reading information of user
                StreamReader swFileReader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                    + "\\RollCallClient\\UserInfo.dat");
                //Read first line of file and then assign it to variable line 1 
                strFirstLine = swFileReader.ReadLine();
                //Read second line of file and then assin it to variable line 2
                strSecondLine = swFileReader.ReadLine();
                //Return false - show that can't read information if line 1 was empty or 'NULL'
                if (strFirstLine.Trim().Length == 0)
                {
                    return false;
                }
                //Return true - show that reading is OK and assign what had been read to line 1 
                else
                {
                    this.strUserName = strFirstLine;
                }
                //Checking if line 2 was empty or NULL
                if (strSecondLine.Trim().Length != 0)
                {
                    //temp variable for holding values that had been read from line 2 - Date Checking 
                    string[] strDatesList = new string[7];
                    strDatesList = strSecondLine.Split(';');
                    //Send values from array day to dayCheking
                    for (int i = 0; i < strDatesList.Length; i++)
                    {
                        //if day have at least one item
                        if (strDatesList[i] != null)
                        {
                            try
                            {
                                //Add to List of day was checked
                                this.clDates.Add(Convert.ToInt32(strDatesList[i]));
                            }
                            catch(Exception ex)
                            {
                                System.Windows.Forms.MessageBox.Show(ex.Message);
                            }
                        }
                    }
                    //If get nothing from file and dayCheking has nothing
                    if (this.clDates == null)
                    {
                        //then return false show that can't load data file
                        return false;
                    }
                }
                //close TextReader
                swFileReader.Close();

            }
            //If any error occur in try block, then can't read data file and return false
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
            }
            //If everything is OK, return true show that Reading was complete
            return true;
        }
        /// <summary>
        /// check to day was checked or not
        /// </summary>
        /// <returns></returns>
        public bool CheckDay()
        {
            int intCurrentDate = (int)DateTime.Now.DayOfWeek;
            foreach (int intDate in clDates)
            {
                if (intDate == intCurrentDate)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
